package club.banyuan.hrms.servlet;

import club.banyuan.hrms.dao.AdminDao;
import club.banyuan.hrms.dao.impl.AdminDbDaoImpl;
import club.banyuan.hrms.domain.Admin;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class AdminDeleteServlet extends HttpServlet {
    private AdminDao adminDao;

    @Autowired
    public AdminDeleteServlet(AdminDao adminDao) {
        this.adminDao = adminDao;
    }

    public AdminDao getAdminDao() {
        return adminDao;
    }


//    解决冲突问题 使用Qualifier 但是这个注解不能在构造器上面 如果用这种方法，要把有参构造方法删除
//    @Autowired
//    @Qualifier("AdminDbDaoImpl")

    //    @Inject
//    @Named("AdminDbDaoImpl")
    public void setAdminDao(AdminDao adminDao) {
        this.adminDao = adminDao;
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String idsParam = request.getParameter("ids");
        if (idsParam == null) {
            return;
        }
        String[] idsArray = idsParam.split(",");
        Integer[] ids = new Integer[idsArray.length];

        int i = 0;
        for (String str : idsArray) {
            ids[i++] = Integer.parseInt(str);
        }

        if (ids.length > 0) {
            adminDao.deleteAdmin(ids);
        }

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);

        // 返回的数据是json格式的，设置ContentType
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().println(JSONObject.toJSONString(resultMap));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
